const path = require('path')
const express = require('express')
const swaggerUI = require('swagger-ui-express')
const swaggerDoc = require('swagger-jsdoc');

//配置swagger-jsdoc
const options = {
    definition: {
        openapi: '3.0.0',
        info: {
            title: '个人博客',
            version: '1.0.0',
            description: `个人博客接口文档`
        }
    },
    // 去哪个路由下收集 swagger 注释
    apis: [path.join(__dirname, '../routes/*.js')],
}

const optionsv2 = {
    definition: {
        openapi: '3.0.0',
        info: {
            title: '个人博客',
            version: '2.0.0',
            description: `个人博客接口文档 v2`
        }
    },
    // 去哪个路由下收集 swagger 注释
    apis: [path.join(__dirname, '../routes/v2/*.js')],
}

// v2
const swaggerSpecv2 = swaggerDoc(optionsv2)
const swaggerJsonv2 = function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpecv2);
}

var swaggerJson = function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpec);
}
const swaggerSpec = swaggerDoc(options)


var swaggerInstall = function (app) {
    if (!app) {
        app = express()
    }
    // 开放相关接口，
    app.get('/v1/swagger.json', swaggerJson);
    // 使用 swaggerSpec 生成 swagger 文档页面，并开放在指定路由
    app.use('/v1/swagger', swaggerUI.serve, swaggerUI.setup(swaggerSpec));
}

var swaggerInstallv2 = function (app) {
    if (!app) {
        app = express()
    }
    // 开放相关接口，
    app.get('/v2/swagger.json', swaggerJsonv2);
    app.use('/v2/swagger', swaggerUI.serve, swaggerUI.setup(swaggerSpecv2));
}

module.exports = {
    swaggerInstall,
    swaggerInstallv2
} 